

local unit_attribute = require 'types.unit_attribute'
local register = unit_attribute.register

local it_sx = {
    ['基础力量'] = true,
    ['基础敏捷'] = true,
    ['基础智力'] = true,
    ['全属性'] = true,
    ['基础攻击'] = true,
    ['生命上限'] = true,
    ['生命恢复'] = true,
    ['物理固伤'] = true,
    ['魔法固伤'] = true,
}

register('装备加成',{
    on_set = function(hero,change)
        print(hero,change)
        for key in pairs(it_sx) do
            local ikey = '装备'..key
            local base = hero:get_data(ikey,0)
            hero:add(key,base * change/100)
        end
    end
})

local player = ac.player.self


ac.game:event '物品-创建'(function(_,item)
    if item.item_class=='装备' then
        item.tip = function(self)
            local hero = player.hero
            local rate = hero and hero:get('装备加成')
            local tip1 = self.tip1
            local tip2 = self.tip2
            if self.name=='噬魂之心' or self.name=='魔剑阿波菲斯' then
                return self.base_tip
            end
            local ex_tip1 = self.ex_tip1
            local ex_tip2 = self.ex_tip2
            local tips = {}
            for a,da in ipairs(self.state) do
                local key = da[1]
                local num = da[2]
                local base = sx_tip(key,num)
                if it_sx[key] and rate and rate>0 then
                    tips[a] = ('%s|cff00ff00(%0.f)|r'):format(base,num*rate/100)
                else
                    tips[a] = base
                end
            end
            tip1 = table.concat(tips,'|n')
            return ('%s%s%s|n|n%s'):format(ex_tip1,tip1,tip2 or '',ex_tip2)
        end
    end
end)


function load_item_state_ad(hero,item,add)
    local state = item and item.state or item or {}
    local ad1 = add * hero:get('装备加成')/100
    for _,da in ipairs(state) do
        local key = da[1]
        local num = da[2]
        if it_sx[key] then
            local ikey = '装备'..key
            hero:add(key,num*ad1)
            hero:set_data(ikey,hero:get_data(ikey,0) + num*add)
        end
    end
end


ac.game:event '单位-添加物品属性'(function(_,hero,item)
    load_item_state_ad(hero,item,1)
end)

ac.game:event '单位-移除物品属性'(function(_,hero,item)
    load_item_state_ad(hero,item,-1)
end)